define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'sort',
                queryParams:function(params) {
                    // 要显示的字段
                    params.fields = '';
                    // 要关联的拓展信息
                    params.extra = 'open_auth';
                    return params;
                },
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('ServerName'),align:'left'},
                        {field: 'appid', title: __('app_id'),align:'left'},
                        {field: 'open_auth.name', title: __('Sid'),align:'left'},
                        {field: 'own_type', title: __('OwnType'), formatter: Controller.api.formatter.own_type,align:'left'},
                        {field: 'qrcode_local_text', title: __('Qrcode'), formatter: Controller.api.formatter.thumb,visible: false},
                        {field: 'auth_status_text',title: __("AuthStatus"), formatter: Controller.api.formatter.auth_status_text},
                        {field: 'status', title: __("Status"), formatter: Controller.api.formatter.status},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Update time'), formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                // 注意：顺序分别是目标表单、提交前置、返回后置 的回调
                Form.api.bindevent($("form[role=form]"), null, function(form, data, ret) {
                    Toastr.success(data.msg ? data.msg : __('Operation completed'));
                    setTimeout(function(){
                        window.location.href = data.url;
                    }, 2000);
                    return false;
                });

                $(document).on("click", '.own_type input', function(){
                    $(".type-item").addClass('hidden');
                    if($(this).val() == 'partner'){
                        $(".type-item.partner").removeClass('hidden');
                    } else if($(this).val() == 'live') {
                        $(".type-item.live").removeClass('hidden');
                    }
                });

            },
            formatter: {
                status: function (value, row, index, custom) {
                    return "<a href='javascript:;' data-url='admin.server/changestatus' class='btn btn-" + (row.status == 'normal' ? "info" : "danger") + " btn-xs btn-change' data-id='" + row.id + "' data-params='" + (row.status == 'normal' ? 'hidden' : 'normal') + "'>" + (row.status == 'normal' ? __('Normal') : __('Close')) + "</a>";
                },
                auth_status_text: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.auth_status == 'authorized') {
                        _color = 'success';
                    }else{
                        _color = 'danger';
                    }
                    return '<span class="text-'+ _color +'">' + row.auth_status_text + '</span>';
                },
                thumb: function (value, row, index) {
                        return '<a href="' + row.qrcode_local_text + '" target="_blank"><img src="' + row.qrcode_local_text + '" alt="" style="max-height:90px;max-width:120px"></a>';
                },
                own_type: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.own_type == 'admin') {
                        _color = 'success';
                    }
                    return '<span class="text-'+ _color +'"> ' + row.own_type_text + '</span>';
                },
                partner: function (value, row, index, custom) {
                    if (row.own_type != 'partner') {
                       row._partner_name = '-';
                    }
                    return row._partner_name;
                }
            },
        },
    };
    return Controller;
});